package org.one.stone.soup.grfxML.plugin.function;


import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class StringMapping extends SimplePlugin {


	public static final int ARG_MAP = 0;

	public static final int ARG_TARGETNAME = 1;

	org.one.stone.soup.grfxML.DataText map = new org.one.stone.soup.grfxML.DataText();
	org.one.stone.soup.grfxML.DataString targetName = new org.one.stone.soup.grfxML.DataString();

	DataString[] options;
	DataState[] triggers;
/**
 * StringMapping constructor comment.
 */
public StringMapping (GrfxMLEngine engine){
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
 try{
	targetName.setValue( getPath()+targetName.getValue(),this );

	options = new DataString[map.size()];
	triggers = new DataState[map.size()];

	for(int loop=0;loop<map.size();loop++)
	{
		String m = map.get(loop).getValue();

		String args[] = org.one.stone.soup.stringhelper.StringArrayHelper.parseFields(m,"=");
		for(int loop2=0;loop2<args.length;loop2++)
		{
			args[loop2].trim();
		}
		triggers[loop] = (DataState)getEngine().getAPI().getField(getPath()+args[0]);
		options[loop] = (DataString)getEngine().getAPI().getField(getPath()+args[1]);
	}
 }catch(LockException le){}
}
/**
 * process method comment.
 */
public void process()
{
	for(int loop=0;loop<triggers.length;loop++)
	{
		if(triggers[loop].getValue()==true)
		{
			try{
				((DataString)getEngine().getAPI().getField(targetName.getValue())).setValue( options[loop].getValue(),this );
			}
			catch(LockException le){}
		}
	}
}
/**
 * register method comment.
 */
public void register(DataLinker store)
{

	map = grfxMLCaster.cast(map,getArg(map,ARG_MAP,store));

	targetName = grfxMLCaster.cast(targetName,getArg(targetName,ARG_TARGETNAME,store));

}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{

	map = grfxMLCaster.replace(map,oldObj,newObj);

	targetName = grfxMLCaster.replace(targetName,oldObj,newObj);

}
/**
 * stop method comment.
 */
public void stop() {}
}
